package com.test.cities.controller;

import com.test.cities.dto.BookDTO;
import com.test.cities.po.Book;
import com.test.cities.response.BaseResponse;
import com.test.cities.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Controller
public class BookSearchController {
    private static final Logger logger = LoggerFactory.getLogger(BookTestController.class);

    @Autowired
    private BookService bookService;

    @PostMapping("/testbook.do/searchByCode")
    public BaseResponse<Book> searchByCode(@RequestBody BookDTO bookDTO){
        logger.debug("Controller查询:"+bookDTO.getCode());
        Book resultBook = bookService.searchByCode(bookDTO.getCode());

        return new BaseResponse<>(0, resultBook, "ok");
    }

    @PostMapping(value = "/testbook.do/insert")
    public BaseResponse<Void> insert(@RequestBody Book book) {
        logger.debug("Controller保存"+book.toString());
        bookService.insert(book);
        return new BaseResponse<>(0, null, "ok");
    }

    @PostMapping(value = "/testbook.do/edit")
    public BaseResponse<Void> edit(@RequestBody Book book) {
        logger.debug("Controller修改"+book.toString());
        bookService.edit(book);
        return new BaseResponse<>(0, null, "ok");
    }
}
